package com.qdu.servlet;

import com.qdu.dao.AdminLoginDao;
import com.qdu.dao.UserLoginDao;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/ulo")
public class UserLogin extends HttpServlet {



	public UserLogin() {
		super();
	}

	public void destroy() {
		super.destroy(); 
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


		String method=request.getParameter("method").trim();
		UserLoginDao ul=new UserLoginDao();
                
		if(method.equals("admin")){
			String name=request.getParameter("name").trim();
			String password=request.getParameter("pwd").trim();
			if(name.equals("")||password.equals("")){
				request.setAttribute("message", "请填写登陆名和密码！");
				request.getRequestDispatcher("admin/login.jsp").forward(request, response);
			}
			else{
				AdminLoginDao ab=new AdminLoginDao();
				int i=ab.userLogin(name, password);
				if(i==1){
					HttpSession session=request.getSession();
					
					session.setAttribute("name", name);
					request.setAttribute("message", "");
					request.getRequestDispatcher("admin/index.jsp").forward(request, response);
				}
				else{
					request.setAttribute("message", "用户名或密码错误！");
					request.getRequestDispatcher("admin/login.jsp").forward(request, response);
				}
			}
		}
		else if(method.equals("user")){
			String name=request.getParameter("username").trim();
			String password=request.getParameter("password").trim();
			int i=ul.userLogin(name, password);
			if(i==1){
				System.out.println("跳转到2登录");
				HttpSession session=request.getSession();
				session.setAttribute("name", name);				
					request.getRequestDispatcher("login2.jsp").forward(request, response);
			}
			else{
				System.out.println("跳转到1登录");
					request.setAttribute("message", "用户名或密码错误！");
					request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		}
	}


	public void init() throws ServletException {
	}

}